load("@rules_gazebo//gazebo:headers.bzl", "gz_configure_header", "gz_export_header")
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [":license"],
    features = [
        "layering_check",
        "parse_headers",
    ],
)

license(
    name = "license",
    package_name = "gz-fuel-tools",
)

licenses(["notice"])

exports_files([
    "package.xml",
    "LICENSE",
    "MODULE.bazel",
])

gz_configure_header(
    name = "Config",
    src = "include/gz/fuel_tools/config.hh.in",
    package_xml = "package.xml",
)

gz_export_header(
    name = "Export",
    out = "include/gz/fuel_tools/Export.hh",
    export_base = "GZ_FUEL_TOOLS",
    lib_name = "gz-fuel-tools",
)

public_headers_no_gen = glob([
    "include/gz/fuel_tools/*.hh",
])

private_headers = glob(["src/*.hh"])

sources = glob(
    ["src/*.cc"],
    exclude = [
        "src/gz.cc",
        "src/*_TEST.cc",
    ],
)

public_headers = public_headers_no_gen + [
    "include/gz/fuel_tools/config.hh",
    "include/gz/fuel_tools/Export.hh",
]

cc_library(
    name = "gz-fuel-tools",
    srcs = sources + private_headers,
    hdrs = public_headers,
    defines = [
        # Json disables exceptions in bazel build:
        # https://github.com/open-source-parsers/jsoncpp/blob/89e2973c754a9c02a49974d839779b151e95afd6/BUILD.bazel#L26
        "JSON_USE_EXCEPTION=0",
    ],
    includes = ["include"],
    visibility = ["//visibility:public"],
    features = [
        # Layering check fails due to use of rules_foreign_cc//cmake in @libzip.
        # See related https://github.com/bazel-contrib/rules_foreign_cc/issues/1221
        "-layering_check",
    ],
    deps = [
        "@com_google_protobuf//:protobuf",
        "@curl",
        "@gz-common",
        "@gz-math",
        "@gz-msgs",
        "@gz-msgs//:gzmsgs_cc_proto",
        "@jsoncpp",
        "@libyaml",
        "@libzip",
        "@tinyxml2",
    ],
)

test_sources = glob(
    include = ["src/*_TEST.cc"],
    exclude = [
        "src/gz_TEST.cc",
        "src/gz_src_TEST.cc",
    ],
)

[cc_test(
    name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
    srcs = [src],
    # Some tests need private headers.
    copts = ["-Wno-private-header"],
    env = {
        "GZ_BAZEL": "1",
    },
    deps = [
        ":gz-fuel-tools",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
        "@gz-common",
        "@gz-common//testing",
        "@gz-utils//:ExtraTestMacros",
    ],
) for src in test_sources]
